package com.sweet.thread.unsafe;

public class UnsafeBuyTicket {
    public static void main(String[] args) {
        BuyTicket station = new BuyTicket();
        new Thread(station,"我").start();
        new Thread(station, "你").start();
        new Thread(station, "他").start();
    }

}
class BuyTicket implements Runnable {

    private int ticketNum = 10;

    private boolean noTicket = false;

    @Override
    public void run() {
        while (!noTicket) {
            // 模拟买票耗时
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            buy();
        }

    }

    // private synchronized void buy () {
    private void buy () {
        if(ticketNum <= 0) {
            noTicket = true;
            return;
        }
        System.out.println(Thread.currentThread().getName()+"买到了第"+ ticketNum-- +"张票");
    }
}